package com.ruoyi.system.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitDelayConfig {
    /*
     * 1、声明正常交换机(coupons_exchange)和正常队列(coupons_queue)，并绑定
     * */
    // 交换机名称
    public static final String ITEM_TOPIC_EXCHANGE = "springboot_topic_exchange";

    // 队列名称
    public static final String ITEM_QUEUE = "coupons_user_queue";
    
    // 声明交换机
    @Bean
    public Exchange couponsExchange() {
        return ExchangeBuilder.topicExchange(ITEM_TOPIC_EXCHANGE).durable(true).build();
    }
    
    // 声明队列
    @Bean
    public Queue couponsQueue() {
        /*
         * 3、正常队列绑定死信交换机
         * */
        return QueueBuilder.durable(ITEM_QUEUE)
                    // deadLetterExchange 死信交换机名称
                        .deadLetterExchange("coupons_dlx_exchange")
                    // deadLetterRoutingKey 死信交换机路由key
                        .deadLetterRoutingKey("coupons.dlx.cancel ")
                    // 4、设置队列的过期时间 10min
//                        .ttl(10 * 1000)
                        .build();
    }
    
    // 绑定交换机和队列
    @Bean
    public Binding bindingCouponsNormal() {
        return BindingBuilder.bind(couponsQueue()).to(couponsExchange()).with("coupons.#").noargs();
    }
    
    
    /*
     * 2、声明死信交换机(coupons_dlx_exchange)和死信队列(coupons_dlx_queue)，并绑定
     * */
    // 声明死信交换机
    @Bean
    public Exchange couponsDlxExchange() {
        return
                ExchangeBuilder.topicExchange("coupons_dlx_exchange").durable(true).build();
    }
    
    // 声明死信队列
    @Bean
    public Queue couponsDlxQueue() {
        return QueueBuilder.durable("coupons_dlx_queue").build();
    }
    
    // 绑定交换机和队列
    @Bean
    public Binding bindingCouponsDlx() {
        return BindingBuilder.bind(couponsDlxQueue()).to(couponsDlxExchange()).with("coupons.dlx.#"). noargs();
    }
}